﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Reviewer.Entities;
using System.Collections.ObjectModel;

namespace Test.Reviewer.Data
{
    /// <summary>
    /// Summary description for GameRepositoryTest
    /// </summary>
    [TestClass]
    public class GameRepositoryTest
    {
        public GameRepositoryTest() { }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        [TestMethod]
        public void Create()
        {
            string testIdentifier = DateTime.Now.Ticks.ToString();
            Game newGame = new Game();
            //not setting game.Id 
            newGame.GameId = Guid.NewGuid();
            newGame.Title = "Test Game " + testIdentifier;
            newGame.Publisher = "Test publisher " + testIdentifier;
            newGame.Developer = "Test developer " + testIdentifier;
            newGame.Genre = 0;
            newGame.MultiplayerInfo = "Test Multi " + testIdentifier;
            newGame.Format = 0;
            newGame.ReleaseDate = DateTime.Now;
            newGame.UpdateDate = DateTime.Now;

            Game savedGame = new global::Reviewer.Data.GameRepository().Create(newGame);

            Assert.IsNotNull(savedGame.Id);
            Assert.IsTrue(savedGame.Id > 0);
            Assert.AreEqual(newGame.GameId, savedGame.GameId);
            Assert.AreEqual(newGame.Title, savedGame.Title);
            Assert.AreEqual(newGame.Publisher, savedGame.Publisher);
            Assert.AreEqual(newGame.Developer, savedGame.Developer);
            Assert.AreEqual(newGame.Genre, savedGame.Genre);
            Assert.AreEqual(newGame.MultiplayerInfo, savedGame.MultiplayerInfo);
            Assert.AreEqual(newGame.Format, savedGame.Format);
            Assert.AreEqual(newGame.ReleaseDate.Date, savedGame.ReleaseDate.Date);
            Assert.AreEqual(newGame.UpdateDate.Date, savedGame.UpdateDate.Date);
        }

        [TestMethod]
        public void Update()
        {
            string testIdentifier = DateTime.Now.Ticks.ToString();

            global::Reviewer.Data.GameRepository repository = new global::Reviewer.Data.GameRepository();
            Collection<Game> games = repository.SelectTopOnReleaseDate(1, false);
            Game existingGame = games[0];

            //updating 
            existingGame.Title = "Test Game " + testIdentifier;
            existingGame.Publisher = "Test publisher " + testIdentifier;
            existingGame.Developer = "Test developer " + testIdentifier;
            existingGame.Genre = 0;
            existingGame.MultiplayerInfo = "Test Multi " + testIdentifier;
            existingGame.Format = 0;
            existingGame.ReleaseDate = DateTime.Now;
            existingGame.UpdateDate = DateTime.Now;

            Game updatedGame = repository.Update(existingGame);

            Assert.AreEqual(existingGame.Id, updatedGame.Id);
            Assert.AreEqual(existingGame.GameId, updatedGame.GameId);
            Assert.AreEqual(existingGame.Title, updatedGame.Title);
            Assert.AreEqual(existingGame.Publisher, updatedGame.Publisher);
            Assert.AreEqual(existingGame.Developer, updatedGame.Developer);
            Assert.AreEqual(existingGame.Genre, updatedGame.Genre);
            Assert.AreEqual(existingGame.MultiplayerInfo, updatedGame.MultiplayerInfo);
            Assert.AreEqual(existingGame.Format, updatedGame.Format);
            Assert.AreEqual(existingGame.ReleaseDate.Date, updatedGame.ReleaseDate.Date);
            Assert.AreEqual(existingGame.UpdateDate.Date, updatedGame.UpdateDate.Date);
        }

        [TestMethod]
        public void SelectOne()
        {
            global::Reviewer.Data.GameRepository repository = new global::Reviewer.Data.GameRepository();
            Collection<Game> games = repository.SelectTopOnReleaseDate(1, false);
            Game existingGame = games[0];

            Game selectByKeyGame = repository.SelectBy(existingGame.Id);

            Assert.IsNotNull(selectByKeyGame);
            Assert.AreEqual(existingGame, selectByKeyGame);
        }

        [TestMethod]
        public void SelectOneByAlternateKey()
        {
            global::Reviewer.Data.GameRepository repository = new global::Reviewer.Data.GameRepository();
            Collection<Game> games = repository.SelectTopOnReleaseDate(1, false);
            Game existingGame = games[0];

            Collection<Game> selectByKeyGame = repository.SelectByAlternateKey(existingGame.GameId);

            Assert.IsNotNull(selectByKeyGame);
            Assert.IsTrue(selectByKeyGame.Count > 0);
        }

        [TestMethod]
        public void SelectAll()
        {
            global::Reviewer.Data.GameRepository repository = new global::Reviewer.Data.GameRepository();

            Collection<Game> selectAllGames = repository.SelectAll();

            Assert.IsNotNull(selectAllGames);
            Assert.IsTrue(selectAllGames.Count > 0);
        }

        [TestMethod]
        public void Delete()
        {
            global::Reviewer.Data.GameRepository repository = new global::Reviewer.Data.GameRepository();
            Collection<Game> games = repository.SelectTopOnReleaseDate(1, false);
            Game existingGame = games[0];

            repository.Delete(existingGame.Id);

            Game selectByKeyGame = repository.SelectBy(existingGame.Id);

            Assert.IsNull(selectByKeyGame);
        }
    }
}
